package com.opNaga.bpmp.managerTest;

import com.opNaga.bpmp.BPMPElement;
import com.opNaga.bpmp.BPMPManager;
import com.opNaga.bpmp.connectingObject.sequenceFlow.SequenceFlow;
import com.opNaga.bpmp.flowObject.activity.Task;
import com.opNaga.bpmp.flowObject.event.EndEvent;
import com.opNaga.bpmp.flowObject.event.StartEvent;
import com.opNaga.bpmp.swimlane.Lane;
import org.junit.Assert;
import org.junit.Test;

import java.util.Iterator;

/**
 * User: Junyi BEI
 * Date: 30/01/2012
 * Time: 21:40
 */
public class BasicManagerFunction {
  @Test
  public void singleInstance() {
    BPMPManager bm = new BPMPManager();
    BPMPElement be = bm.createInstance("Pool");
    Assert.assertEquals("p1", be.getName());
  }

  @Test
  public void multipleInstances() {
    BPMPManager bm = new BPMPManager();
    BPMPElement be1 = bm.createInstance("pool");
    Assert.assertEquals("p1", be1.getName());
    BPMPElement be2 = bm.createInstance("p");
    Assert.assertEquals("p2", be2.getName());
  }

  @Test
  public void getInstance() {
    BPMPManager bm = new BPMPManager();
    BPMPElement be1 = bm.createInstance("p");
    BPMPElement be2 = bm.createInstance("l");
    String[] names = {"p1", "l1"};
    BPMPElement[] bes = bm.getInstances(names);
    Assert.assertEquals(be1, bes[0]);
    Assert.assertEquals(be2, bes[1]);
  }

  @Test
  public void leftover() {
    BPMPManager bm = new BPMPManager();
    BPMPElement be1 = bm.createInstance("pool");
    Assert.assertEquals("p1", be1.getName());
    BPMPElement be2 = bm.createInstance("p");
    Assert.assertEquals("p2", be2.getName());
    BPMPElement be3 = bm.createInstance("p");
    Assert.assertEquals("p3", be3.getName());
    BPMPElement be4 = bm.createInstance("p");
    Assert.assertEquals("p4", be4.getName());
    BPMPElement be5 = bm.createInstance("p");
    Assert.assertEquals("p5", be5.getName());
    BPMPElement be6 = bm.createInstance("p");
    Assert.assertEquals("p6", be6.getName());
    be5.destroy();
    be3.destroy();
    BPMPElement be7 = bm.createInstance("p");
    Assert.assertEquals("p3", be7.getName());
    BPMPElement be8 = bm.createInstance("p");
    Assert.assertEquals("p5", be8.getName());
  }

  @Test
  public void parameter() {
    BPMPManager bm = new BPMPManager();
    BPMPElement be1 = bm.createInstance("p", "-n=test");
    Assert.assertEquals("test", be1.getName());
    BPMPElement be2 = bm.createInstance("l", "-p=test", "-n=lane");
    Assert.assertEquals(be1, ((Lane)be2).getRoot());
    BPMPElement be3 = bm.createInstance("se", "-in=lane", "-n=start");
    BPMPElement be4 = bm.createInstance("t", "-in=lane", "-n=task", "-is=start");
    BPMPElement be5 = bm.createInstance("ee", "-in=lane", "-n=end", "-is=task");
    Iterator<BPMPElement> beIt1 = ((StartEvent) be3).iterateOutwardSequenceFlows();
    int count1 = 0;
    BPMPElement sf1 = null;
    while(beIt1.hasNext()) {
      sf1 = beIt1.next();
      count1++;
    }
    Assert.assertEquals(1, count1);
    Assert.assertEquals(be3, ((SequenceFlow) sf1).getSource());
    Assert.assertEquals(be4, ((SequenceFlow) sf1).getTarget());
    Iterator<BPMPElement> beIt2 = ((Task) be4).iterateInwardSequenceFlows();
    int count2 = 0;
    BPMPElement sf2 = null;
    while(beIt2.hasNext()) {
      sf2 = beIt2.next();
      count2++;
    }
    Assert.assertEquals(1, count2);
    Assert.assertEquals(be3, ((SequenceFlow) sf2).getSource());
    Assert.assertEquals(be4, ((SequenceFlow) sf2).getTarget());
    Iterator<BPMPElement> beIt3 = ((Task) be4).iterateOutwardSequenceFlows();
    int count3 = 0;
    BPMPElement sf3 = null;
    while(beIt3.hasNext()) {
      sf3 = beIt3.next();
      count3++;
    }
    Assert.assertEquals(1, count3);
    Assert.assertEquals(be4, ((SequenceFlow) sf3).getSource());
    Assert.assertEquals(be5, ((SequenceFlow) sf3).getTarget());
    Iterator<BPMPElement> beIt4 = ((EndEvent) be5).iterateInwardSequenceFlows();
    int count4 = 0;
    BPMPElement sf4 = null;
    while(beIt4.hasNext()) {
      sf4 = beIt4.next();
      count4++;
    }
    Assert.assertEquals(1, count4);
    Assert.assertEquals(be4, ((SequenceFlow) sf4).getSource());
    Assert.assertEquals(be5, ((SequenceFlow) sf4).getTarget());
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(be3, be4).getClass()));
    Assert.assertTrue(SequenceFlow.class.isAssignableFrom(bm.getLink(be4, be5).getClass()));
    be3.mute();
    be5.mute();
    Assert.assertNull(bm.getLink(be3, be5));
  }
}
